package com.aisile.crowd_funding.system.controller;

import com.aisile.crowd_funding.common.annotion.Log;
import com.aisile.crowd_funding.common.controller.BaseController;
import com.aisile.crowd_funding.common.utils.MD5Utils;
import com.aisile.crowd_funding.common.utils.R;
import com.aisile.crowd_funding.common.utils.StringUtils;
import com.aisile.crowd_funding.system.mapper.SysUserMapper;
import com.aisile.crowd_funding.system.pojo.SysMenu;
import com.aisile.crowd_funding.system.pojo.SysUser;
import com.aisile.crowd_funding.system.service.MenuService;
import com.aisile.crowd_funding.system.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sound.midi.Soundbank;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.BindException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @标题 LoginController
 * @作者 Mr.chen
 * @创建日期 2018/12/4 16:43
 * @版本号 1.0
 **/
@Controller
public class LoginController extends BaseController {
    @Autowired
    UserService userService;
    @Autowired
    MenuService menuService;

    @RequestMapping(value = {"/",""})
    public String defaultPage(){
        new Object();
        return "redirect:index";
    }
    @RequestMapping("login")
    public String login(){
        return "login";
    }

    /**
     * 跳转首页方法
     * @return
     */
    @RequestMapping("index")
    public String index(Model model){
        SysUser loginUser = getLoginUser();
        model.addAttribute("username",  loginUser.getUsername());

        //根据当前登录的用户  查询出   对应的角色  查询出  对应的 菜单
        //  type in (0,1)   0目录    1菜单    2按钮
        List<SysMenu> menus = menuService.selectMenuListByUserId(loginUser.getUserId());
        //   map .key  装目录    .value  对应菜单
        Map<SysMenu,List<SysMenu>> map = new LinkedHashMap<>();
        // 一级目录
        List<SysMenu> muliList = new ArrayList<>();
        for(SysMenu sysMenu:menus){
            if(0==sysMenu.getType()){
                muliList.add(sysMenu);
            }
        }

        for(SysMenu sysMenu : muliList){
            List<SysMenu> childList = new ArrayList<>();
            for(SysMenu child : menus){
                if(child.getParentId()==sysMenu.getMenuId()){
                    childList.add(child);
                }
            }
            map.put(sysMenu,childList);
        }
        model.addAttribute("menuMap",map);
        return "index";
    }

    @Log("用户登录")
    @RequestMapping("loginSign")
    @ResponseBody
    public R loginSign(String username,String password){
        //校验参数
        if(StringUtils.isBlank(username)){
            return R.error("用户名不能为空");
        }
        if(StringUtils.isBlank(password)){
            return R.error("密码不能为空");
        }
        //  认证
        Subject subject = SecurityUtils.getSubject();
        //登录 认证信息
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, MD5Utils.encrypt(password));
        try {
            subject.login(usernamePasswordToken);
        }catch (UnknownAccountException e){
            R.error("未查询到该用户");
        }catch (LockedAccountException e){
            R.error("用户认证失败");
        }catch (AuthenticationException e){
            R.error("登录失败");
        }catch (Exception e){
            R.error("登录失败");
        }
        return R.ok();
    }


    /**
     * 主菜单方法
     * @return
     */
    @RequestMapping("main")
    public String main(){
        return "system/user/uploadPhoto";
    }




}
